﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace View
{
    public partial class Orders : Form
    {
        private Menu menu;
        private Controller.Orders control;

        public Orders(Menu menu,string customerphone,string name)
        {
            this.menu = menu;
            control = new Controller.Orders(customerphone);
            InitializeComponent();
            this.comboBox1.DataSource = Controller.Menu.getMenu();
            comboBox2.DataSource = control.getDishes();
            this.label2.Text = name+"   "+customerphone;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            menu.Show();
            this.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string msg = control.addOrder(comboBox1.Text,textBox3.Text);
            dataGridView1.DataSource = control.getOrders();
            comboBox2.DataSource = control.getDishes();
            Message x = new Message(msg);
            x.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string msg = control.removeOrder(comboBox2.Text);
            dataGridView1.DataSource = control.getOrders();
            comboBox2.DataSource = control.getDishes();
            Message x = new Message(msg);
            x.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string msg = control.createInvoice();
            writeInovice(msg);
            menu.Show();
            Message x = new Message(msg);
            x.Show();
            this.Dispose();
        }

        private void writeInovice(string msg)
        {
            string date = DateTime.Now.ToString("dd/MM/yyyy");
            string time = DateTime.Now.ToString("HH:mm:ss tt");
            string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string file = mydocpath + @"\no" + label2.Text + ".txt";

            TextWriter tw = new StreamWriter(file);

            int start = 0;
            int end=0;
            msg += "\n";
            while (start < msg.Length)
            {
                if (msg[end] == '\n')
                {
                    // write a line of text to the file
                    tw.WriteLine(msg.Substring(start, end - start));
                    end++;
                    start = end;
                }
                else
                {
                    end++;
                }
            }
            // close the stream
            tw.Close();
        }

    }
}
